package com.home.job.zh2.job1;


public class MyTime {
    private int hour;
    private int min;
    private int sec;

    public MyTime() {
        this.hour = 0;
        this.min = 0;
        this.sec = 0;
    }
    public MyTime(int hour) {
        this.hour = hour;
        this.min = 0;
        this.sec = 0;
    }
    public MyTime(int hour, int min) {
        this.hour = hour;
        this.min = min;
        this.sec = 0;
    }
    public MyTime(int hour, int min, int sec) {
        this.hour = hour;
        this.min = min;
        this.sec = sec;
    }
    public MyTime(MyTime time) {
        this.hour = time.hour;
        this.min = time.min;
        this.sec = time.sec;
    }

    public String toUniversalString(){
        String error = hasError();
        if (!"".equals(error)) {
            return error;
        }
        return formatTime(this.hour);
    }

    @Override
    public String toString(){
        String error = hasError();
        if (!"".equals(error)) {
            return error;
        }
        int temp = this.hour;
        String noon = "AM";
        if (this.hour > 12) {
            temp = this.hour - 12;
            noon = "PM";
        } else if (this.hour == 12) {
            noon = "PM";
        }
        return formatTime(temp) + " " + noon;
    }

    public String formatTime(int hour) {
        String hours = hour < 10 ? "0" + hour : String.valueOf(hour);
        String min = this.min < 10 ? "0" + this.min : String.valueOf(this.min);
        String sec = this.sec < 10 ? "0" + this.sec : String.valueOf(this.sec);
        return hours + ":" + min + ":" + sec;
    }

    public String hasError() {
        String error = "";
        if (this.hour < 0 || this.hour > 24) {
            error += "hour must be 0-24\n";
        }
        if (this.min < 0 || this.min > 59) {
            error += "minute must be 0-59\n";
        }
        if (this.sec < 0 || this.sec > 59) {
            error += "second must be 0-24\n";
        }
        return error;
    }

    public static void main(String[] args) {
        MyTime t1 = new MyTime();
        MyTime t2 = new MyTime(2);
        MyTime t3 = new MyTime(21, 34);
        MyTime t4 = new MyTime(12, 25, 42);
        MyTime t5 = new MyTime(t4);
        MyTime t6 = new MyTime(15, 74, 99);


        System.out.println("Constructed with");
        System.out.println("t1: all arguments defaulted");
        System.out.printf("    %s\n",t1.toUniversalString());
        System.out.printf("    %s\n", t1.toString());

        System.out.println("t2: hour specified; minute and second defaulted");
        System.out.printf("    %s\n",t2.toUniversalString());
        System.out.printf("    %s\n", t2.toString());

        System.out.println("t3: hour and minute specified; second defaulted");
        System.out.printf("    %s\n",t3.toUniversalString());
        System.out.printf("    %s\n", t3.toString());

        System.out.println("t4: hour, minute and second specified");
        System.out.printf("    %s\n",t4.toUniversalString());
        System.out.printf("    %s\n", t4.toString());

        System.out.println("t5: MyTime Object t4 specified");
        System.out.printf("    %s\n",t5.toUniversalString());
        System.out.printf("    %s\n", t5.toString());

        System.out.println("t6: invalid values");
        System.out.printf("%s\n",t6.toUniversalString());
    }
}
